<template>
  <div class="dashboard-container" >
    <!-- 标题 -->
    <h1 style="text-align: center;margin-bottom: 40px;font-size: 35px;color: #486180">房间类型管理</h1>

    <!-- 新增房间类型对话框 -->
    <el-dialog title="新增" :visible.sync="addButtonController" width="30%" @close="closeRoomType('ruleForm')">
      <el-form label-width="20%" :model="addList" ref="ruleForm">
        <!-- 注释掉的房间类型表ID输入框 -->
        <!--        <el-form-item label="房间类型表ID" prop="roomTypeTableId">-->
        <!--          <el-input v-model="addList.roomTypeTableId" maxlength="20" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型表ID"/>-->
        <!--        </el-form-item>-->
        <!-- 注释掉的房间类型ID输入框 -->
        <!--        <el-form-item label="房间类型ID" prop="roomTypeId">-->
        <!--          <el-input v-model="addList.roomTypeId" maxlength="20" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型ID"/>-->
        <!--        </el-form-item>-->
        <!-- 房间类型名称输入框 -->
        <el-form-item label="房间类型名称" prop="roomTypeName">
          <el-input v-model="addList.roomTypeName" maxlength="11" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型名称"/>
        </el-form-item>
        <!-- 房间价格输入框 -->
        <el-form-item label="房间价格" prop="roomPrice">
          <el-input v-model="addList.roomPrice" maxlength="18" show-word-limit style="width: 80%" clearable placeholder="请输入房间价格"/>
        </el-form-item>
        <!-- 备注输入框 -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="addList.remark" maxlength="100" show-word-limit style="width: 80%" clearable placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <!-- 确定按钮，点击触发addRoomType方法 -->
        <el-button type="info" plain @click="addRoomType('ruleForm')">确 定</el-button>
        <!-- 取消按钮，关闭对话框 -->
        <el-button plain @click="closeRoomType('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 修改房间类型对话框 -->
    <el-dialog title="修改" :visible.sync="updateButtonController" width="30%" @close="closeRoomType('ruleForm')">
      <el-form label-width="20%" :model="updateList" ref="ruleForm">
        <!-- 注释掉的房间类型表ID输入框 -->
        <!--                <el-form-item label="房间类型表ID" prop="roomTypeTableId">-->
        <!--                  <el-input v-model="updateList.roomTypeTableId" maxlength="20" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型表ID"/>-->
        <!--                </el-form-item>-->
        <!-- 注释掉的房间类型ID输入框 -->
        <!--        <el-form-item label="房间类型ID" prop="roomTypeId">-->
        <!--          <el-input v-model="updateList.roomTypeId" maxlength="20" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型ID"/>-->
        <!--        </el-form-item>-->
        <!-- 房间类型名称输入框 -->
        <el-form-item label="房间类型名称" prop="roomTypeName">
          <el-input v-model="updateList.roomTypeName" maxlength="11" show-word-limit style="width: 80%" clearable placeholder="请输入房间类型名称"/>
        </el-form-item>
        <!-- 房间价格输入框 -->
        <el-form-item label="房间价格" prop="roomPrice">
          <el-input v-model="updateList.roomPrice" maxlength="18" show-word-limit style="width: 80%" clearable placeholder="请输入房间价格"/>
        </el-form-item>
        <!-- 备注输入框 -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="updateList.remark" maxlength="100" show-word-limit style="width: 80%" clearable placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <!-- 确定按钮，点击触发updateRoomType方法 -->
        <el-button type="info" plain @click="updateRoomType('ruleForm')">确 定</el-button>
        <!-- 取消按钮，关闭对话框 -->
        <el-button plain @click="closeRoomType('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 删除确认对话框 -->
    <el-dialog title="删除" :visible.sync="deleteButtonController" width="30%" @close="closeDelete('ruleForm')">
      <!-- 确认删除提示信息 -->
      <div style="margin: 10px"><span>请确认是否删除</span></div>
      <span slot="footer" class="dialog-footer">
        <!-- 确定删除按钮，点击触发deleteRoomType方法 -->
          <el-button type="info" plain @click="deleteRoomType('ruleForm')">确定</el-button>
        <!-- 取消删除按钮，关闭对话框 -->
          <el-button plain @click="closeDelete('ruleForm')">取消</el-button>
      </span>
    </el-dialog>

    <!-- 新增按钮，点击触发addButton方法 -->
    <el-button type="primary" plain style="width:80px;margin-bottom:30px;margin-left:10% ;" @click="addButton">新增</el-button>
    <!-- 房间价格输入框，用于筛选 -->
    <el-input v-model="roomPrice" placeholder="房间价格" name="roomPrice" type="text" clearable style="margin-left:40px ;width: 180px"/>
    <!-- 房间类型名称输入框，用于筛选 -->
    <el-input
      v-model="roomTypeName"
      placeholder="房间类型名称"
      name="roomTypeName"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />
    <!-- 查询按钮，点击触发selectRoomType方法 -->
    <el-button type="primary" plain style="width:80px;margin-bottom:30px;margin-left:40px;" @click="selectRoomType">查询</el-button>
    <!-- 重置按钮，点击触发checkRoomType方法 -->
    <el-button  style="width:80px;margin-bottom:30px;margin-left:40px;" @click="checkRoomType">重置</el-button>

    <!-- 房间类型表格 -->
    <el-table :data="roomTypeList" height="659px" border style="width: 100%;margin-top: 20px">
      <!-- 房间类型ID列 -->
      <el-table-column align="center" fixed prop="roomTypeId" label="房间类型ID"/>
      <!-- 房间类型名称列 -->
      <el-table-column align="center" fixed prop="roomTypeName" label="房间类型名称"/>
      <!-- 房间价格列 -->
      <el-table-column align="center" fixed prop="roomPrice" label="房间价格"/>
      <!-- 备注列 -->
      <el-table-column align="center" fixed prop="remark" label="备注"/>
      <!-- 操作列 -->
      <el-table-column align="center" fixed label="操作">
        <template v-slot="scope">
          <!-- 修改按钮，点击触发updateButton方法并传入当前行数据 -->
          <el-button class="el-icon-edit" circle @click="updateButton(scope.row)"></el-button>
          <!-- 删除按钮，点击触发deleteButton方法并传入当前行数据 -->
          <el-button class="el-icon-delete" circle @click="deleteButton(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { deleteRoomType, insertRoomType, selectAllRoomType, updateRoomType } from '@/api/roomType'

export default {
  name: 'user',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  data(){
    return{
      roomPrice:'',
      roomTypeName:'',
      roomTypeList: [],
      addButtonController:false,
      updateButtonController:false,
      deleteButtonController:false,
      deleteRoomTypeId:'',
      addList:{
        // 房间类型表ID（注释掉，暂不使用）
        // roomTypeTableId:'',
        // 房间类型ID（注释掉，暂不使用）
        // roomTypeId:'',
        roomTypeName:'',
        roomPrice:'',
        remark:''
      },
      updateList:{
        roomTypeTableId:'',
        roomTypeId:'',
        roomTypeName:'',
        roomPrice:'',
        remark:''
      },
    }
  },
  mounted() {
    // 组件挂载后自动查询房间类型列表
    this.selectRoomType()
  },
  methods:{
    // 查询房间类型列表
    selectRoomType(){
      selectAllRoomType({
        roomPrice:this.roomPrice,
        roomTypeName:this.roomTypeName
      }).then(
        response => {
          console.log(response.data)
          this.roomTypeList = response.data
        }
      )
    },
    // 显示新增对话框并重置表单
    addButton(){
      this.addList = {
        // roomTypeTableId:'',
        // roomTypeId:'',
        roomTypeName:'',
        roomPrice:'',
        remark:''
      }
      this.addButtonController = true
    },
    // 显示修改对话框并填充当前行数据
    updateButton(row){
      this.updateList = {
        roomTypeTableId:row.roomTypeTableId,
        roomTypeId:row.roomTypeId,
        roomTypeName:row.roomTypeName,
        roomPrice:row.roomPrice,
        remark:row.remark
      }
      this.updateButtonController = true
    },
    // 显示删除确认对话框并记录要删除的房间类型ID
    deleteButton(row){
      this.deleteRoomTypeId = row.roomTypeId
      this.deleteButtonController = true
    },
    // 提交新增表单
    addRoomType(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          insertRoomType({
            // roomTypeTableId:this.addList.roomTypeTableId,
            // roomTypeId:this.addList.roomTypeId,
            roomTypeName: this.addList.roomTypeName,
            roomPrice: this.addList.roomPrice,
            remark: this.addList.remark
          }).then(
            response => {
              this.addButtonController = false
              this.$message.success("新增成功！"); // 添加成功提示
              this.selectRoomType()
            }
          )
        } else {
          return false
        }
      })
    },
    // 重置筛选条件并重新查询
    checkRoomType() {
      this.roomPrice = ''
      this.roomTypeName = ''
      this.selectRoomType()
    },
    // 重置表单并关闭对话框
    closeRoomType(formName) {
      this.$refs[formName].resetFields();
      this.addButtonController = false;
      this.updateButtonController = false;
    },
    // 提交修改表单
    updateRoomType(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateRoomType({
            roomTypeTableId: this.updateList.roomTypeTableId,
            roomTypeId: this.updateList.roomTypeId,
            roomTypeName: this.updateList.roomTypeName,
            roomPrice: this.updateList.roomPrice,
            remark: this.updateList.remark
          }).then(
            async response => {
              this.updateButtonController = false
              this.selectRoomType()
            })
        } else {
          return false;
        }
      });
    },
    // 提交删除请求
    deleteRoomType(formName) {
      deleteRoomType({
        roomTypeId: this.deleteRoomTypeId
      }).then(
        async response => {
          this.deleteButtonController = false
          this.selectRoomType()
        })
    },
    // 关闭所有对话框
    closeDelete() {
      this.addButtonController = false
      this.updateButtonController = false
      this.deleteButtonController = false
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
